package com.songlanyun.modules.wechat.controller.admin;

import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.goods.entity.Goods;
import com.songlanyun.modules.goods.service.GoodsService;
import com.songlanyun.modules.shop.entity.ShopEntity;
import com.songlanyun.modules.shop.service.ShopService;
import com.songlanyun.modules.user.controller.AbstractController;
import com.songlanyun.modules.wechat.entity.WechatLiveGoods;
import com.songlanyun.modules.wechat.service.WechatLiveGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


@RestController
@RequestMapping("admin/wechatLiveGoods")
public class WeChatLiveGoodsAdminController extends AbstractController {
    @Autowired
    private WechatLiveGoodsService wechatLiveGoodsService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ShopService shopService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R page(@RequestParam Map<String, Object> params) {
        PageUtils page = wechatLiveGoodsService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R infoByShopId(@PathVariable("id") Long id) {
        WechatLiveGoods liveGoods = wechatLiveGoodsService.getById(id);
        if (liveGoods == null) {
            return R.error("直播商品不存在");
        }
        Goods goods = goodsService.getById(liveGoods.getProductId());
        if (goods == null) {
            return R.error("店铺商品不存在");
        }
        ShopEntity shop = shopService.getById(goods.getShopId());
        goods.setShop(shop);
        liveGoods.setGoods(goods);
        return R.ok().put("data", liveGoods);
    }


    @PostMapping("/create")
    public R create(@RequestBody WechatLiveGoods entity) {
        if (entity.getProductId() == null) {
            return R.error("关联商品错误");
        }
        Goods goods = goodsService.getById(entity.getProductId());
        if (goods == null) {
            return R.error("关联商品错误");
        }
        if (goods.getVerifyStatus().getCode() != 1) {
            return R.error("商品未审核");
        }
        WechatLiveGoods obj = wechatLiveGoodsService.create(entity);
        return R.ok().put("data", obj);
    }


    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") String id) {
        wechatLiveGoodsService.remove(Long.parseLong(id));
        return R.ok();
    }


    @PutMapping("/update")
    public R update(@RequestBody WechatLiveGoods entity) {
        WechatLiveGoods obj = wechatLiveGoodsService.modify(entity);
        return R.ok().put("data", obj);
    }

    @PostMapping("/synchro")
    public ResponseEntity<Object> synchroWxOlLive(@RequestBody Integer[] ids) {
        wechatLiveGoodsService.synchroWxOlLive(Arrays.asList(ids));
        return new ResponseEntity<>(HttpStatus.OK);
    }


}
